package memorphic.samplr.views
{
	import flash.display.DisplayObject;
	
	import memorphic.samplr.ISamplrAppProps;
	import memorphic.samplr.SamplrStateManager;
	import memorphic.samplr.views.popups.AddSelectionPopup;
	import memorphic.slide.IStateView;
	import memorphic.slide.SlidePresModelBase;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;

	public class HeaderPresModel extends SlidePresModelBase
	{
		[Bindable]
		public var appProps:ISamplrAppProps;
		
		public function HeaderPresModel(view:ISamplrAppProps)
		{
			super(view as IStateView);
			appProps = view;
		}
		
		public function search(searchString:String):void
		{
			appProps.galleryController.search(searchString);
		}
		
		public function addSelectionToGallery():void
		{
			var addSelectionPopup:AddSelectionPopup = new AddSelectionPopup();
			addSelectionPopup.dataProvider = appProps.galleries;
			addSelectionPopup.addEventListener(CloseEvent.CLOSE, onPopupClose);
			PopUpManager.addPopUp(addSelectionPopup,Header(appProps).parentApplication as DisplayObject,true);
			PopUpManager.centerPopUp(addSelectionPopup);
		}
		
		private function onPopupClose(evt:CloseEvent):void
		{
			var asp:AddSelectionPopup = AddSelectionPopup(evt.target);
			asp.removeEventListener(CloseEvent.CLOSE, onPopupClose);
			if (evt.detail == Alert.OK)
			{
				appProps.galleryController.addSelectionToGallery(asp.selectedGalleryTitle);
			}
			PopUpManager.removePopUp(asp);
		}
		
		public function removeSelectionFromGallery():void
		{
			appProps.galleryController.removeSelectionFromGallery();
		}
		
		public function deleteGallery():void
		{
			appProps.galleryController.deleteGallery();
		}
		
		public function switchView():void
		{
			switch(view.stateManager.stateURI)
			{
				case SamplrStateManager.BROWSE:
					view.stateManager.stateURI = SamplrStateManager.BROWSE_SLIDESHOW;
				break;
				case SamplrStateManager.BROWSE_SLIDESHOW:
					view.stateManager.stateURI = SamplrStateManager.BROWSE;
				break;
				case SamplrStateManager.GALLERY:
					view.stateManager.stateURI = SamplrStateManager.GALLERY_SLIDESHOW;
				break;
				case SamplrStateManager.GALLERY_SLIDESHOW:
					view.stateManager.stateURI = SamplrStateManager.GALLERY;
				break;
				case SamplrStateManager.GALLERY_EDIT:
					view.stateManager.stateURI = SamplrStateManager.GALLERY_SLIDESHOW;
				break;
			}
		}
	}
}